package com.matt.controller.pad.care;

import com.google.common.collect.ImmutableMap;
import com.matt.commons.dto.sso.StaffDTO;;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.service.care.HLCareBaseService;
import com.matt.service.care.po.HLCareBaseRecordPO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.matt.foundation.constant.RequestConstant.TICKET;

public class CareBaseController<T1, T2 extends HLCareBaseService<T1, P>, P extends HLCareBaseRecordPO> {

    protected T2 baseService;
    public void setBaseService(T2 service){
        this.baseService = service;
    }

    @Autowired
    protected MemVerificationCache cache;

    @ApiOperation(value = "获取单日列表")
    @GetMapping("/daily_records")
    public StatusResponse getList(@RequestHeader(TICKET) String ticket, String childId, Date daily,Boolean diaper){
        StaffDTO staff = cache.getStaff(ticket);

        List<T1>  ret = baseService.dailyRecords(staff.getAreaId(), staff.getId(), childId, daily,
                (diaper == null? new HashMap<>() : ImmutableMap.of("diaper", diaper))
        );

        return StatusResponse.success(ret);
    }

    @ApiOperation(value = "获取列表")
    @GetMapping("/list")
    public StatusResponse getList(@RequestHeader(TICKET) String ticket, String childId, PagerQuery pagerQuery){
        StaffDTO staff = cache.getStaff(ticket);

        PageRequest pageRequest = PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize());

        return StatusResponse.success( baseService.page(staff.getAreaId(), staff.getId(), childId, pageRequest));
    }

    @ApiOperation(value = "增加一条记录（使用childId）或多个记录（使用childIds）, 响应结果都是数组")
    @PostMapping()
    public StatusResponse add(@RequestHeader(TICKET) String ticket, P param){
        StaffDTO staff = cache.getStaff(ticket);

        if (param.getChildIds() != null && param.getChildIds().length > 0){
            List<T1> ret = baseService.addAll(staff.getAreaId(), staff.getId(), param);

            return StatusResponse.success(ret);
        }else{
            T1 ret = baseService.add(staff.getAreaId(), staff.getId(), param);

            List<T1> tmp = new ArrayList<>();
            tmp.add(ret);

            return StatusResponse.success(tmp);
        }
    }

    @ApiOperation(value = "获取一个对象")
    @GetMapping()
    public StatusResponse get(@RequestHeader(TICKET) String ticket, String childId, String id){
        StaffDTO staff = cache.getStaff(ticket);
        return StatusResponse.success(baseService.get(staff.getAreaId(), staff.getId(), childId, id));
    }

    @ApiOperation(value = "逻辑删除一个对象")
    @DeleteMapping()
    public StatusResponse delete(@RequestHeader(TICKET) String ticket, String childId, String id ){
        StaffDTO staff = cache.getStaff(ticket);

        baseService.delete(staff.getAreaId(), staff.getId(), childId, id);

        return StatusResponse.success();
    }

}
